package com.atguigu.dga.dolphinscheduler.service.impl;

import com.atguigu.dga.dolphinscheduler.bean.TDsTaskInstance;
import com.atguigu.dga.dolphinscheduler.mapper.TDsTaskInstanceMapper;
import com.atguigu.dga.dolphinscheduler.service.TDsTaskInstanceService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuxu
 * @since 2023-10-13
 */
@Service
@DS("dolphinscheduler")
public class TDsTaskInstanceServiceImpl extends ServiceImpl<TDsTaskInstanceMapper, TDsTaskInstance> implements TDsTaskInstanceService {

    @Override
    public List<TDsTaskInstance> selectList(String assessDate) {
        List<TDsTaskInstance> list = list(
                new QueryWrapper<TDsTaskInstance>().exists(
                        "SELECT 1 FROM ( " +
                                "     SELECT MAX(id) max_id FROM t_ds_task_instance " +
                                "     WHERE state=7 AND DATE_FORMAT(start_time , '%Y-%m-%d') = '" + assessDate + "' " +
                                "     GROUP BY NAME  " +
                                "  )t2  " +
                                "  WHERE t_ds_task_instance.id =  t2.max_id")
        );
        return list;
    }

    //有报错
    @Override
    public List<TDsTaskInstance> selectFailedTask(String assessDate, String testName) {
        List<TDsTaskInstance> list = list(
                new QueryWrapper<TDsTaskInstance>().eq("name", testName)
                        .eq("date_format(start_time, '%Y-%m-%d')", assessDate)
                        .eq("state", 6)
        );
        return list;
    }

    @Override
    public List<TDsTaskInstance> selectBeforeNDaysInstance(String tsetName, String startformat, long currentTime) {
        QueryWrapper<TDsTaskInstance> queryWrapper = new QueryWrapper<TDsTaskInstance>()
                .exists(
                        "SELECT 1 FROM (\n" +
                                "SELECT MAX(id) max_id FROM t_ds_task_instance\n" +
                                "WHERE \n" +
                                "  state=7 \n" +
                                "AND \n" +
                                "  DATE_FORMAT(start_time , '%Y-%m-%d') >= '" + startformat + "'\n" +
                                "AND\n" +
                                "  DATE_FORMAT(start_time , '%Y-%m-%d') < '" + currentTime + "'  \n" +
                                "AND   \n" +
                                "  NAME = '" + tsetName + "'\n" +
                                "  \n" +
                                "GROUP BY NAME , DATE_FORMAT(start_time , '%Y-%m-%d')\n" +
                                ") t1 \n" +
                                "WHERE t_ds_task_instance.id = t1.max_id "
                );
        List<TDsTaskInstance> list = list(queryWrapper);
        return list;
    }
}
